import router from './router'
import store from './store'
import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { getToken } from '@/utils/auth' // get token from cookie
// import getPageTitle from '@/utils/get-page-title'

NProgress.configure({ showSpinner: false }) // NProgress Configuration

const whiteList = ['/login'] // no redirect whitelist

// 前置路由守卫
router.beforeEach(async(to, from, next) => {
  NProgress.start()
  // console.log(store.getters.token)
  // 接受一个token
  const token = store.getters.token && getToken()
  // 如果有token
  if (token) {
    // 直接放行
    next()
  } else {
    if (whiteList.includes(to.path)) {
      // 在白名单里直接放行
      next()
    } else {
      Message.error('token过期')
      // 没有token  去login页面
      next('/login')
    }
  }
  NProgress.done()
})

// 后置路由守卫
router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})
